﻿using System;
using System.Collections.Generic;
using System.Data; //for DataTable
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//using LLBLGenWebConsultationDAL;
//using LLBLGenWebConsultationDAL.TypedViewClasses;
//using LLBLGenWebConsultationDAL.DaoClasses;
//using LLBLGenWebConsultationDAL.CollectionClasses;
//using LLBLGenWebConsultationDAL.EntityClasses;
//using LLBLGenWebConsultationDAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using SD.LLBLGen.Pro.ORMSupportClasses.SelfServicingSpecific;
using SD.LLBLGen.Pro.DQE.SqlServer;
using MyHealthOnline.Dal.Generated.CollectionClasses;
using MyHealthOnline.Dal.Generated.HelperClasses;
using MyHealthOnline.Dal.Generated.EntityClasses;
//using WebConsult.HelperClasses;
//using WebConsult.EntityClasses;
//using WebConsult.TypedViewClasses;
namespace MyHealthOnline
{
    public partial class CreateSpecialist : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // C#
            DisciplineCollection disciplines = new DisciplineCollection();
            SortExpression sorter = new SortExpression();
            sorter.Add(DisciplineFields.Title | SortOperator.Ascending);
            //disciplines.Fill(20, sorter, false);
            //ExcludeIncludeFieldsList exclude = new ExcludeIncludeFieldsList(DisciplineFields);
            disciplines.GetMulti(null, 0, sorter);
            disciplineDDL.DataSource = disciplines;
            disciplineDDL.DataBind();

            //InvoicesTypedView invoices = new InvoicesTypedView();
            //// simply fetch the 2nd page of 25 rows. We specify no filter, 
            //// no group by collection and we specify 0 for the max number of elements to return which means
            //// all of them. We also specify that we don't care about duplicate rows. 
            //// We do specify a sort expression, as paging without a sorter isn't really reliable. 
            //SortExpression sorter = new SortExpression();
            //sorter.Add(InvoicesFields.OrderId | SortOperator.Ascending);
            //invoices.Fill(0, sorter, true, null, null, null, 2, 25);

            //// Now that invoices is filled with data, we'll show it in the viewer.
            //Viewer v = new Viewer();
            //v.BindDataTable(invoices);
            //v.ShowDialog();
        }

        protected void SpButonAnuleaza_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Administrator/AdminPageContent.aspx");
        }

        protected void clearTextFields()
        {
            CodSpText.Text = "";
            SpNameText.Text = "";
            Date5.Text = "";
            MailText.Text = "";
            SpParolaText.Text = "";
            SpConfirmParolaText.Text = "";
            NrTelText.Text = "";
            AdresaText.Text = "";
            OrasText.Text = "";
            TaraText.Text = "";
        }

        protected void SpButonSalveaza_Click(object sender, EventArgs e)
        {
            //MODIFY DATA IN THE DB

            //STEP 1: open transaction
            // Create the transaction object, pass the isolation level and give it a name
            Transaction trans = new Transaction(IsolationLevel.ReadCommitted, "TransInserSpecialist"); //Serializable - A range lock is placed on the System.Data.DataSet preventing other users from updating or inserting rows into dataset until the trasnaction is complete
            try
            {
                //STEP 2: modify data
                SpecialistEntity newSpecialist = new SpecialistEntity();
                //newSpecialist.Id = CodSpText.Text.ToString();
                //newSpecialist.Active = Convert.ToInt32(ActivCheckBox.Checked);////////???????
                newSpecialist.DisciplineId = Convert.ToInt32( disciplineDDL.SelectedValue);
                //newSpecialist.Role = "Specialist";
                newSpecialist.Name = SpNameText.Text.ToString();
                newSpecialist.Birthday = Date5.Text.ToString();
                newSpecialist.Sex = SpSexDropDownList.SelectedValue.ToString();
                newSpecialist.Email = MailText.Text.ToString();
                newSpecialist.Password = SpParolaText.Text.ToString();
                newSpecialist.Organization = null;
                newSpecialist.Phone = NrTelText.Text.ToString();
                newSpecialist.Address = AdresaText.Text.ToString();
                newSpecialist.City = OrasText.Text.ToString();
                newSpecialist.Country = TaraText.Text.ToString();

                //STEP 3: Add modified entity to transaction
                trans.Add(newSpecialist);

                //STEP 4: Save entity
                newSpecialist.Save();

                //STEP 5: Commit transaction if everything ok
                trans.Commit();
            }
            catch (Exception ex)
            {
                trans.Rollback();
                throw ex;
            }
            finally
            {
                trans.Dispose();
                clearTextFields();
            }
        }
    }
}